﻿using IBLL;
using IDAL;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class OrderInfoBLL : IOrderInfoBLL
    {
        private ICustomerInfoDAL _customerInfoDAL;
        private ICustomerAddressDAL _customerAddressDAL;
        private IProductInfoDAL _productInfoDAL;
        private IOrderInfoDAL _orderInfoDAL;

        public OrderInfoBLL(
            ICustomerInfoDAL customerInfoDAL
            , IOrderInfoDAL orderInfoDAL
            , IProductInfoDAL productInfoDAL
            , ICustomerAddressDAL customerAddressDAL
            )
        {
            _customerInfoDAL = customerInfoDAL;
            _productInfoDAL = productInfoDAL;
            _orderInfoDAL = orderInfoDAL;
            _customerAddressDAL = customerAddressDAL;
        }

        #region 获取全部订单表的方法 (GetOrderInfoH)
        /// <summary>
        /// 获取全部订单表的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetOrderInfoDTO> GetOrderInfoH(int page, int limit,  out int count)
        {
            var data = from o in _orderInfoDAL.GetEntities()
                       join c in _customerInfoDAL.GetEntities()
                       on o.CustomerID equals c.Id
                       into OCtemp
                       from oc in OCtemp.DefaultIfEmpty()

                       join ca in _customerAddressDAL.GetEntities()
                       on oc.AddressId equals ca.Id
                       into CAOCtemp
                       from caoc in CAOCtemp.DefaultIfEmpty()
                       select new GetOrderInfoDTO
                       {
                           Id = o.Id,
                           CustomerName = oc.CustomerName,
                           Address = caoc.Address,
                           OrderAmount = o.OrderAmount,
                           OrderImage = o.OrderImage,
                           CreateTime = o.CreateTime
                       };

            //计算数据总数
            count = data.Count();
            //分页(降序)
            var listpage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();

            return listpage;
        }
        #endregion

        #region 获取已完成订单表的方法 (GetOrderInfoH)
        /// <summary>
        /// 获取全部订单表的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="customerId"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetOrderInfoDTO> GetOrderInfoTrue(int page, int limit, string customerId, out int count)
        {
            var data = from o in _orderInfoDAL.GetEntities().Where(d => d.IsDelete == true && d.CustomerID == customerId)
                       join c in _customerInfoDAL.GetEntities()
                       on o.CustomerID equals c.Id
                       into OCtemp
                       from oc in OCtemp.DefaultIfEmpty()

                       join ca in _customerAddressDAL.GetEntities()
                       on oc.AddressId equals ca.Id
                       into CAOCtemp
                       from caoc in CAOCtemp.DefaultIfEmpty()
                       select new GetOrderInfoDTO
                       {
                           Id = o.Id,
                           CustomerName = oc.CustomerName,
                           Address = caoc.Address,
                           OrderAmount = o.OrderAmount,
                           OrderImage = o.OrderImage,
                           CreateTime = o.CreateTime
                       };

            //计算数据总数
            count = data.Count();
            //分页(降序)
            var listpage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();

            return listpage;
        }
        #endregion

        #region 按顾客获取订单表的方法 (GetOrderInfos)
        /// <summary>
        /// 按顾客获取订单表的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="customerId"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetOrderInfoDTO> GetOrderInfo(int page, int limit, string customerId,out int count)
        {
            var data = from o in _orderInfoDAL.GetEntities().Where(d => d.IsDelete == false && d.CustomerID == customerId)
                       join c in _customerInfoDAL.GetEntities()
                       on o.CustomerID equals c.Id
                       into OCtemp
                       from oc in OCtemp.DefaultIfEmpty()

                       join ca in _customerAddressDAL.GetEntities()
                       on oc.AddressId equals ca.Id
                       into CAOCtemp
                       from caoc in CAOCtemp.DefaultIfEmpty()
                       select new GetOrderInfoDTO
                       {
                           Id = o.Id,
                           CustomerName = oc.CustomerName,
                           Address = caoc.Address,
                           OrderAmount = o.OrderAmount,
                           OrderImage = o.OrderImage,
                           CreateTime = o.CreateTime
                       };

            //计算数据总数
            count = data.Count();
            //分页(降序)
            var listpage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();

            return listpage;
        }
        #endregion

        #region 创建订单的方法 (CreateOrderInfo)
        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="orde">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        public bool CreateOrderInfo(OrderInfo orde, out string msg)
        {
            if (string.IsNullOrWhiteSpace(orde.CustomerID))
            {
                msg = "顾客名称不能为空";
                return false;
            }

            orde.Id = Guid.NewGuid().ToString();//用户id
            orde.CreateTime = DateTime.Now;//设置时间

            bool IsSuccess = _orderInfoDAL.CreateEntity(orde);//调用方法
            msg = IsSuccess ? $"添加成功" : "添加失败";
            
            return IsSuccess;
        }
        #endregion

        #region 软删除订单的方法 (DeleteOrderInfo)
        /// <summary>
        /// 删除订单的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteOrderInfo(string id)
        {
            OrderInfo orde = _orderInfoDAL.GetEntityById(id);
            if (orde == null)
            {
                return false;
            }
            orde.IsDelete = true;
            orde.DeleteTime = DateTime.Now;
            _orderInfoDAL.UpdateEntity(orde);
            return true;
        }
        #endregion

        #region 删除订单的方法 (DeleteOrderInfo)
        /// <summary>
        /// 删除订单的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelOrderInfo(string id)
        {
            OrderInfo orde = _orderInfoDAL.GetEntityById(id);
            if (orde == null)
            {
                return false;
            }
            _orderInfoDAL.DeleteEntity(orde);
            return true;
        }
        #endregion

        #region 批量删除订单的方法 (DeleteListOrderInfo)
        /// <summary>
        /// 批量删除的方法
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteListOrderInfo(List<string> ids)
        {
            List<OrderInfo> ordeList = _orderInfoDAL.GetEntities().Where(it => ids.Contains(it.Id)).ToList();
            foreach (var item in ids)
            {
                OrderInfo orde = _orderInfoDAL.GetEntityById(item);
                if (ordeList == null)
                {
                    return false;
                }
                orde.IsDelete = true;
                orde.DeleteTime = DateTime.Now;

                _orderInfoDAL.UpdateEntity(orde);
            }
            return true;
        }
        #endregion

        #region 修改订单的方法 (UpdateOrderInfo)
        /// <summary>
        /// 修改用户的方法
        /// </summary>
        /// <param name="orde"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdateOrderInfo(OrderInfo orde, out string msg)
        {
            if (string.IsNullOrWhiteSpace(orde.CustomerID))
            {
                msg = $"顾客名称不能为空";
                return false;
            }
            OrderInfo entity = _orderInfoDAL.GetEntityById(orde.Id);
            if (entity.Id == null)
            {
                msg = "ID不存在";
                return false;
            }

            entity.CustomerID = orde.CustomerID;
            entity.OrderAmount = orde.OrderAmount;
            entity.OrderImage = orde.OrderImage;

            bool IsSuccess = _orderInfoDAL.UpdateEntity(entity);//调用方法

            msg = IsSuccess ? $"修改成功" : "修改失败";

            return IsSuccess;
        }
        #endregion

        #region 根据ID获取数据返回赋值 (GetOrderInfoById)
        /// <summary>
        /// 根据ID获取数据返回赋值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OrderInfo GetOrderInfoById(string id)
        {
            return _orderInfoDAL.GetEntityById(id);
        }
        #endregion

        #region 获取数据库列表赋值下拉框 (GetSelectOption)
        /// <summary>
        /// 获取数据库列表赋值下拉框
        /// </summary>
        /// <returns></returns>
        public object GetSelectOption()
        {
            var customerData = _customerInfoDAL.GetEntities().Where(it => it.IsDelete == false).Select(d => new
            {
                value = d.Id,
                title = d.CustomerName,
            });

            return new
            {
                customerData = customerData,
            };
        }
        #endregion
    }
}
